package org.devops

def switchs(name,appname,baseurl,workdir){
    //name与switchs保持一致
    if(name == "public"){
        return public1(appname,baseurl,workdir,name)
    }else if(name == "dotnet"){
        return teka(appname,baseurl,workdir,name)
    }else{
        return "name不匹配00508315.png"
    }
}

//公共-前端参数
def public1(appname,baseurl,workdir,pname){
    //编译参数
        tmp = """
cd ${workdir}
sed -i 's#^VUE_APP_API_BASE_URL.*#VUE_APP_API_BASE_URL=${baseurl}#' .env
sed -i 's#^VUE_APP_VERNAME.*#VUE_APP_VERNAME=${appname}#' .env
rm -rf dist
rm -rf dist.tar.gz
yarn install
yarn run build
tar zcf dist.tar.gz dist
    """
    //部署脚本
    tmp1 = """
cd /aizhixin/frontend/${pname}/tar && tar -xf dist.tar.gz
cd /aizhixin/frontend/${pname}/web && mv * /aizhixin/frontend/${pname}/tar
cd /aizhixin/frontend/${pname}/web && rm -rf *
cd /aizhixin/frontend/${pname}/tar/dist && mv * /aizhixin/frontend/${pname}/web/
    """
    //删除旧资源
    tmp2="""
[[ ! -d /aizhixin/frontend/${pname}/tar ]] && mkdir -p /aizhixin/frontend/${pname}/tar
[[ ! -d /aizhixin/frontend/${pname}/web ]] && mkdir -p /aizhixin/frontend/${pname}/web
[[ ! -f /aizhixin/frontend/${pname}/web/index.html ]] && touch /aizhixin/frontend/${pname}/web/index.html
cd /aizhixin/frontend/${pname}/tar/ && rm -rf *
"""
    //上传路径
    tmp3 = "/aizhixin/frontend/${pname}/tar"
return [tmp.trim(),tmp1.trim(),tmp2,tmp3]
}

//teka-前端参数
def teka(appname,baseurl,workdir,pname){
    //编译参数
        tmp = """
cd ${workdir}
sed -i 's#^VUE_APP_API_BASE_URL.*#VUE_APP_API_BASE_URL=${baseurl}#' .env
sed -i 's#^VUE_APP_VERNAME.*#VUE_APP_VERNAME=${appname}#' .env
rm -rf dist
rm -rf dist.tar.gz
yarn install
yarn run build
tar zcf dist.tar.gz dist
    """
    //部署脚本
    tmp1 = """
cd /aizhixin/frontend/${pname}/tar && tar -xf dist.tar.gz
cd /aizhixin/frontend/${pname}/web && mv * /aizhixin/frontend/${pname}/tar
cd /aizhixin/frontend/${pname}/web && rm -rf *
cd /aizhixin/frontend/${pname}/tar/dist && mv * /aizhixin/frontend/${pname}/web/
    """
    //删除旧资源
    tmp2="""
[[ ! -d /aizhixin/frontend/${pname}/tar ]] && mkdir -p /aizhixin/frontend/${pname}/tar
[[ ! -d /aizhixin/frontend/${pname}/web ]] && mkdir -p /aizhixin/frontend/${pname}/web
[[ ! -f /aizhixin/frontend/${pname}/web/index.html ]] && touch /aizhixin/frontend/${pname}/web/init.txt
[[ ! -f /aizhixin/frontend/${pname}/tar/init.txt ]] && touch /aizhixin/frontend/${pname}/tar/init.txt
cd /aizhixin/frontend/${pname}/tar/ && rm -rf *
"""
    //上传路径
    tmp3 = "/aizhixin/frontend/${pname}/tar"
return [tmp.trim(),tmp1.trim(),tmp2,tmp3]
}